class MatchesController < ApplicationController
  
  # GET /matches
  # GET /matches.xml
  def index
    @matches = Match.find(:all)

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @matches }
    end
  end

  # GET /matches/1
  # GET /matches/1.xml
  def show
    @match = Match.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @match }
    end
  end

  # GET /matches/new
  # GET /matches/new.xml
  def new
    @match = Match.new
    
    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @match }
    end
  end

  # GET /matches/1/edit
  def edit
    @match = Match.find(params[:id])
  end

  # POST /matches
  # POST /matches.xml
  def create
    @match = Match.new(params[:match])
    @match.user_id = current_user.id

    flash[:notice] = 'Match was successfully updated.'
    
    if @match.save
      if @match.date >= Time.now
        render :partial => 'update_matches.rjs'
      else 
        render :partial => 'update_historical_matches.rjs'
      end
    end
  end

  # PUT /matches/1
  # PUT /matches/1.xml
  def update
    @match = Match.find(params[:id])
    
    flash[:notice] = 'Match was successfully updated.'
    
    respond_to do |format|
      if @match.update_attributes(params[:match])
        format.html { redirect_to(welcome_index_url) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @match.errors, :status => :unprocessable_entity }
      end
    end
  end
  
  def create_scores 
    @match = Match.find(params[:id])
  end
  
  # DELETE /matches/1
  # DELETE /matches/1.xml
  def destroy
    @match = Match.find(params[:id])
    @match.destroy
    
    flash[:notice] = 'Match was removed'
    
    if request.xhr? 
      
      
    else
      respond_to do |format|
        format.html { redirect_to(welcome_index_url) }
        format.xml  { head :ok }
      end
    end
  end
end
